import * as Cesium from "cesium"
import { DrawerBase, EventsConfig } from "./DrawerBase"

/**
 * @author Oo青花鱼
 * @description 折线绘制
 * @example
 * //实例化Drawer
 * const drawer = new PolylineDrawer(viewer);
 * drawer.start() //开始绘制
 * drawer.end() //结束绘制
 * drawer.clear() //清空绘制的图形
 * drawer.destroy() //销毁实例
 * drawer.data //访问绘制的Entity
 * drawer.onClose = (entity:Cesium.Entity)=>{
 *    console.log('绘制了一个折线',entity)
 * }
 */
export class PolylineDrawer extends DrawerBase {
  protected helpers: Cesium.Entity.ConstructorOptions[] = [
    //鼠标位置
    {
      position: new Cesium.CallbackPositionProperty(() => {
        return this.mousePosition
      }, false),
      point: {},
    },
    //折线
    {
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length >= 1) {
            return [...this.featurePositions.value, this.mousePosition]
          } else return undefined
        }, false),
      },
    },
  ]
  protected events: EventsConfig = {
    cesiumEvents: [
      //左键点击事件
      {
        type: "click",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          //拾取坐标
          const position = this.scene.pickPosition(e.position)
          position && this.featurePositions.value.push(position)
        },
      },
      //右键点击事件
      {
        type: "rightclick",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          //拾取坐标
          const position = this.scene.pickPosition(e.position)
          position && this.featurePositions.value.push(position)

          //根据顶点绘制出图形
          this.drawFeature({
            polyline: {
              positions: this.featurePositions.value,
            },
          })

          //初始化折线绘制状态
          this.featurePositions.value = []
        },
      },
    ],
  }
}
